// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.process.parts;

import java.beans.PropertyChangeEvent;

import org.dengues.commons.ui.gef.SimpleHtmlFigure;
import org.dengues.designer.ui.editors.editmanager.CompNodeLabelCellEditorLocator;
import org.dengues.designer.ui.editors.editmanager.CompNodeLabelDirectEditManager;
import org.dengues.designer.ui.process.models.CompConnectionLabel;
import org.dengues.designer.ui.process.policies.CompConnectionLabelDirectEditPolicy;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.tools.DirectEditManager;
import org.eclipse.jface.viewers.TextCellEditor;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * @since 2007-10-8 下午10:11:07
 */
public class CompConnectionLabelPart extends AbstractProcessPart {

    private DirectEditManager directEditManager;

    private SimpleHtmlFigure htmlFig;

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.process.parts.AbstractProcessPart#getCastModel()
     */
    @Override
    protected CompConnectionLabel getCastModel() {
        return (CompConnectionLabel) getModel();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
     */
    @Override
    protected IFigure createFigure() {
        String text = getCastModel().getCompName();
        htmlFig = new SimpleHtmlFigure();
        htmlFig.setText(text);
        return htmlFig;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractEditPart#refreshVisuals()
     */
    @Override
    protected void refreshVisuals() {
        Point loc = getCastModel().getLocation().getCopy();
        htmlFig.setText(getCastModel().getCompName());
        Dimension size = htmlFig.getPreferredSize();
        Point textOffset = new Point();
        textOffset.y = loc.y;
        textOffset.x = loc.x;
        Rectangle rectangle = new Rectangle(textOffset, size);
        ((GraphicalEditPart) getParent()).setLayoutConstraint(this, getFigure(), rectangle);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
     */
    @Override
    protected void createEditPolicies() {
        installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, new CompConnectionLabelDirectEditPolicy());
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
     */
    public void propertyChange(PropertyChangeEvent evt) {
        refresh();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractEditPart#performRequest(org.eclipse.gef.Request)
     */
    @Override
    public void performRequest(Request req) {
        if (req.getType() == RequestConstants.REQ_DIRECT_EDIT) {
            if (directEditManager == null) {
                directEditManager = new CompNodeLabelDirectEditManager(this, TextCellEditor.class,
                        new CompNodeLabelCellEditorLocator(htmlFig));
            }
            directEditManager.show();
        }
        super.performRequest(req);
    }

}
